package com.hao.eduorder.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hao.commonutils.ordervo.CourseWebVoOrder;
import com.hao.commonutils.ordervo.UcenterMemberOrder;
import com.hao.eduorder.client.EduClient;
import com.hao.eduorder.client.UcenterClient;
import com.hao.eduorder.entity.Order;
import com.hao.eduorder.mapper.OrderMapper;
import com.hao.eduorder.service.OrderService;
import com.hao.eduorder.utils.OrderNoUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 订单 服务实现类
 * </p>
 *
 * @author hao
 * @since 2021-01-20
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {

    @Autowired
    private EduClient eduClient;

    @Autowired
    private UcenterClient ucenterClient;

    @Override
    public String createOrders(String courseId, String userId) {
        //远程调用获取用户和课程信息
        CourseWebVoOrder course = eduClient.getCourseInfoOrder(courseId);
        UcenterMemberOrder user = ucenterClient.getUserInfoOrder(userId);
        Order order = new Order();
        String orderNo = OrderNoUtil.getOrderNo();
        order.setOrderNo(orderNo);
        order.setCourseId(course.getId());
        order.setCourseCover(course.getCover());
        order.setCourseTitle(course.getTitle());
        order.setTeacherName(course.getTeacherName());
        order.setTotalFee(course.getPrice());
        order.setMemberId(user.getId());
        order.setMobile(user.getMobile());
        order.setNickname(user.getNickname());

        order.setStatus(0);
        order.setPayType(1);
        this.save(order);
        return orderNo;
    }

    @Override
    public Order getByOrderNo(String orderNo) {
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        wrapper.eq("order_no", orderNo);
        Order order = this.getOne(wrapper);
        return order;
    }

    @Override
    public boolean isBuyCourse(String courseId, String memberId) {
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id", courseId);
        wrapper.eq("member_id", memberId);
        //支付状态
        wrapper.eq("status", 1);
        int count = this.count(wrapper);
        if (count > 0) {
            return true;
        } else {
            return false;
        }
    }
}
